package com.deyang.mycommon.ui.activity.basics;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.deyang.mycommon.R;
import com.deyang.mycommon.config.IntentConfig;
import com.deyang.mycommon.ui.baseui.BaseActivity;
import com.deyang.mycommon.widget.TopBar;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WebViewActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar topBar;
    @BindView(R.id.pb_wvLoad)
    ProgressBar pbWvLoad;
    @BindView(R.id.webView)
    WebView webView;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_web_view);
        ButterKnife.bind(this);

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        initWebView();

        Intent intent = getIntent();
        String url = intent.getStringExtra(IntentConfig.WEB_VIEW_URL);
        String title = intent.getStringExtra(IntentConfig.WEB_VIEW_Title);
        topBar.setCenterText(title);
        webView.loadUrl(url);
    }

    private void initWebView() {
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        WebSettings setting = webView.getSettings();
        //设置webview支持javascript脚本
        setting.setJavaScriptEnabled(true);
        setting.setSupportZoom(true); //手势缩放
        setting.setDisplayZoomControls(false);//隐藏Zoom缩放按钮
        setting.setRenderPriority(WebSettings.RenderPriority.HIGH);
        setting.setBlockNetworkImage(false);
        //设置支持缩放
        setting.setBuiltInZoomControls(true);

        // 设置setWebChromeClient对象
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 使用自己的WebView组件来响应Url加载事件，而不是使用默认浏览器器加载页面
                webView.loadUrl(url);
                // 消耗掉这个事件。Android中返回True的即到此为止吧,事件就会不会冒泡传递了，我们称之为消耗掉
                return true;
            }
        });//对webview页面加载管理、如url重定向

        webView.addJavascriptInterface(null, "game");
        webView.clearCache(true);//支持缓存

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    pbWvLoad.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    pbWvLoad.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    pbWvLoad.setProgress(newProgress);//设置进度值
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
    }
}